/**
 * Copyright (c) 2008 Trent Hoeppner
 * 
 * This file is part of BillCalc.
 * 
 * BillCalc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BillCalc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BillCalc.  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.trentech.billcalc.event;

import java.util.Date;

import com.trentech.billcalc.FinanceManager;

/**
 * PayEvent transfers money from one person to another person or a biller.
 */
public class PayEvent extends MoneyEvent {
    
    /**
     * The person or biller to be paid.
     */
    private String paidEntity;


    /**
     * Constructor for PayEvent.
     * 
     * @param   date    the date that the bill was paid.  Cannot be null.
     * @param   name    the name of the person who is paying.  Cannot be 
     *                  null or empty.
     * @param   amount  the amount of money to be given.  Must be >= 0.
     * @param   biller  the person or biller to be paid.  Cannot be null or
     *                  empty.
     */
    public PayEvent(Date date, String name, int amount, String biller) {
        super(date, name, amount);
        this.paidEntity = biller;
    }


    /**
     * {@inheritDoc}
     */
    public int compareTo(Event other) {
        int value = super.compareTo(other);
        if (value != 0) {
            return value;
        }
        
        PayEvent otherCasted = (PayEvent) other;
        value = paidEntity.compareTo(otherCasted.getPaidEntity());
        if (value != 0) {
            return value;
        }

        return 0;
    }
    
    
    /**
     * Pays the given amount to the owed person from the payer, such that
     * the debt owed is reduced by amount.
     * 
     * @param   manager the FinancialManager to help process this Event.  
     *                  Cannot be null.
     */
    public void process(FinanceManager manager) {
        manager.residentPaysSomeone(getName(), paidEntity, getAmount());
    }

    
    /**
     * Returns the name of the person or biller to pay.
     * 
     * @return  the name of the person or biller to pay.  Will not be null or 
     *          empty.
     */
    public String getPaidEntity() {
        return paidEntity;
    }
    
    
    /**
     * Sets the name of the person or biller to pay.
     * 
     * @param   paidEntity  the name of the person or biller to pay.  Cannot be 
     *                      null or empty.
     */
    public void setPaidEntity(String paidEntity) {
        String oldPaidEntity = this.paidEntity;
        this.paidEntity = paidEntity;
        getSupport().firePropertyChange("paidEntity", oldPaidEntity, paidEntity);
        setSaved(false);
    }


    /**
     * {@inheritDoc}
     */
    public void validate(String propertyName, FinanceManager manager) 
        throws InvalidResultException {
        
        super.validate(propertyName, manager);
        if (propertyName.equals("name")) {
            resident(propertyName, getName(), manager);
        }
        if (propertyName.equals("paidEntity")) {
            notNullOrEmptyString(propertyName, getPaidEntity());
            residentOrBiller(propertyName, getPaidEntity(), manager);
        }
    }
}
